<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
import BScorll from "better-scroll";
export default {
  props: {
    probeType: {
      type: Number,
      default() {
        return 0;
      }
    },
    pullUpLoad: {
      type: Boolean,
      default() {
        return false;
      }
    }
  },
  data() {
    return {
      scorll: null
    };
  },
  mounted() {
    // 1. 创建Bscroll对象
    this.scorll = new BScorll(this.$refs.wrapper, {
      click: true,
      probeType: this.probeType,
      pullUpLoad: this.pullUpLoad
    });

    // 2. 监听滚动位置
    if (this.probeType == 2 || this.probeType == 3) {
      this.scorll.on("scroll", position => {
        this.$emit("scroll", position);
      });
    }

    // 下拉超过阀值(下拉加载更多)
    if (this.pullUpLoad) {
      this.scorll.on("pullingUp", () => {
        this.$emit("pullingUp");
      });
    }
  },
  methods: {
    /* 
    滑动至哪里
     */
    scrollTo(x, y, time = 300) {
      this.scorll && this.scorll.scrollTo(x, y, time);
    },
    /* 
    下拉加载更多
     */
    finishPullUp() {
      this.scorll && this.scorll.finishPullUp();
    },
    /* 
    重新计算渲染宽高
     */
    refresh() {
      this.scorll && this.scorll.refresh();
    },
    /* 
  记录 Y 轴的位置
  */
    getScrollY() {
      return this.scorll ? this.scorll.y : 0;
    }
  }
};
</script>

<style lang="scss" scoped></style>
